package lib.self.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import androidx.annotation.ColorInt;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ListView;

import lib.self.ConstEx;

/**
 * description: 界面工具类
 * author: Steven Xiao
 * date: 2017/4/28
 */
public class UiUtil {
    private static Integer sStatusBarHeight = null;

    public static int dpToPx(Context context, int dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) ((dpValue * scale) + 0.5f);
    }

    public static int dpToPx(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) ((dpValue * scale) + 0.5f);
    }

    /**
     * 设罢dialog acitivty 的window全屏显示
     *
     * @param activity
     */
    public static void setWindowToFullScreen(Activity activity) {
        WindowManager manager = activity.getWindowManager();
        Display display = manager.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        setWindowAttr(activity, size.x, size.y, Gravity.CENTER);
    }

    /**
     * 设置activity的window宽匹配屏幕宽度
     *
     * @param activity
     * @param gravity  对齐方式. 比如 Gravity.CENTER
     */
    public static void setWindowWidthMatchScreen(Activity activity, int gravity) {
        WindowManager manager = activity.getWindowManager();
        Display display = manager.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        setWindowAttr(activity, size.x, ConstEx.KInvalidValue, gravity);
    }

    public static void setWindowAttr(Activity activity, int width, int height, int gravity) {
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();
        if (width != ConstEx.KInvalidValue) {
            params.width = width;
        }
        if (height != ConstEx.KInvalidValue) {
            params.height = height;
        }
        params.gravity = gravity;
        activity.getWindow().setAttributes(params);
    }

    public static int computeStatusBarHeight(Context context) {
        Class<?> c = null;
        Object obj = null;
        java.lang.reflect.Field field = null;
        int x = 0;
        int statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
            return statusBarHeight;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusBarHeight;
    }

    /**
     * 获取状态栏高度
     *
     * @return 未经缩放处理的原始px
     */
    public static int getStatusBarHeight(Context context) {
        if (sStatusBarHeight == null) {
            sStatusBarHeight = computeStatusBarHeight(context);
        }
        return sStatusBarHeight;
    }

    public static void setStatusBar(View statusBar, @ColorInt int color) {
        if (color == 0) {
            return;
        }
        LayoutParams params = statusBar.getLayoutParams();
        params.height = getStatusBarHeight(statusBar.getContext());
        statusBar.setLayoutParams(params);
        statusBar.setBackgroundColor(color);
    }
}
